﻿Imports System.Data.SqlClient

Public Class Perfil_MenuADO
    ReadOnly MiConexion As New ConexionADO()
    ReadOnly cnx As New SqlConnection(MiConexion.GetCnx)

    Public Function pr_Copy_Perfiles(ByVal p_codnive As String, ByVal p_codnivecopy As String) As Boolean
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            cn.Open()
            Using oTran As SqlTransaction = cn.BeginTransaction(IsolationLevel.Serializable)
                Dim sSql As String
                sSql = "INSERT INTO dbo.Nivel_Menu (cod_nive,key1,parent, crea, modi,elimi,cons,espe,tlbsno) SELECT @codnive,key1,parent,crea,modi,elimi,cons,espe,tlbsno FROM dbo.Nivel_Menu WHERE cod_nive=@codnivecopy"
                Dim cmdSelect As New SqlCommand(sSql, cn) With {.Transaction = oTran}
                cmdSelect.Parameters.AddWithValue("@codnive", p_codnive)
                cmdSelect.Parameters.AddWithValue("@codnivecopy", p_codnivecopy)
                Try
                    cmdSelect.ExecuteNonQuery()
                    oTran.Commit()
                    Return True
                Catch myException As Exception
                    MsgBox("Perfil_MenuADO (Copy_Perfiles) --> " & myException.Message, MsgBoxStyle.Critical)
                    oTran.Rollback()
                    Return False
                End Try
            End Using
        End Using
    End Function

    Public Function pr_Get_PerfilMenu(ByVal p_codnive As String) As DataTable
        Dim dt As New DataTable
        Dim sql As String = "SELECT menu.codcla,menu.codpar,Menu.nomopc,ISNULL(Niveles_Menu.opccre,0) as opccre,ISNULL(Niveles_Menu.visible,0) as visible,ISNULL(Niveles_Menu.opcmod,0) as opcmod,ISNULL(Niveles_Menu.opceli,0) as opceli, " & _
                            "ISNULL(Niveles_Menu.opccon,0) as opccon, icoopc FROM dbo.Menu LEFT JOIN (SELECT codcla,codpar,opccre,opcmod,opceli,opccon,Cast(1 as int) as visible FROM dbo.Niveles_Menu WHERE codnive='" & p_codnive & "') Niveles_Menu ON Menu.codcla=Niveles_Menu.codcla AND Menu.codpar=Niveles_Menu.codpar"
        Try
        cnx.Open()
        Using da As New SqlDataAdapter(Sql, cnx)
            da.Fill(dt)
        End Using
        cnx.Close()
        Return dt
        Catch myException As Exception
            MsgBox("Get_PerfilMenu --> " & myException.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Function pr_GetAccesoOpcion(ByVal p_codnive As String, ByVal p_codacce As String) As Boolean
        Dim valida As Boolean = False
        Dim sql As String = String.Format("Select Count(*) as valido from dbo.Niveles_Menu INNER JOIN dbo.Menu ON Niveles_Menu.codcla = Menu.codcla AND Niveles_Menu.codpar = Menu.codpar WHERE Niveles_Menu.codnive=@codnive AND Menu.codacc=@codacc ", p_codnive, p_codacce)
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            Try
                cn.Open()
                Dim cmd As New SqlCommand(sql, cn)
                Using cmd
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.Add("@codnive", SqlDbType.Char, 2)
                    cmd.Parameters("@codnive").Value = p_codnive
                    cmd.Parameters.Add("@codacc", SqlDbType.Char, 30)
                    cmd.Parameters("@codacc").Value = p_codacce
                    Dim t As Integer = CInt(cmd.ExecuteScalar())
                    If t > 0 Then
                        valida = True
                    End If
                    Return valida
                End Using
            Catch ex As Exception
                cn.Close()
                MsgBox("Perfil_MenuADO (pr_GetAccesoOpcion) --> " & ex.Message, MsgBoxStyle.Critical)
                Return Nothing
            End Try
        End Using
    End Function



End Class
